package com.danco.training.zaneuskaya.controller;

import java.io.FileWriter;
import java.io.IOException;




import java.io.Serializable;












import com.danco.training.zaneuskaya.domain.entity.Claim;
import com.danco.training.zaneuskaya.domain.entity.storage.Bookshop;
import com.danco.training.zaneuskaya.domain.service.storage.BookStorageService;
import com.danco.training.zaneuskaya.domain.service.storage.ClaimStorageService;
import com.danco.training.zaneuskaya.view.IOUtil;


public class ClaimController implements Serializable{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 3686885504220073158L;

	private ClaimStorageService claimStorageService = Bookshop.getBookshopInstance().getClaimStorageService();
	
	private BookStorageService bookStorageService = Bookshop.getBookshopInstance().getBookStorageService();
	private BookController bookController = new BookController();
	

	public void printClaimList() {
		IOUtil.write("Claims in ClaimStorage");
		for(Claim claim:claimStorageService.getClaimList() ){
			IOUtil.write(claimInfo(claim));
		}
	}

	public void printClaimListSortedByAlphabet() {
		IOUtil.write("Claims in ClaimStorage sorted by alphabet");
		for(Claim claim:claimStorageService.sortClaimsByAlphabet(claimStorageService.getClaimList())){
			IOUtil.write(claimInfo(claim));
			}

	}

	public void printClaimListSortedByClaimAmount() {
		IOUtil.write("Claims in ClaimStorage sorted by claim amount");
		for(Claim claim:claimStorageService.sortClaimsByClaimAmount(claimStorageService.getClaimList())){
			IOUtil.write(claimInfo(claim));
			}
		

	}

	public void createClaim(){
		IOUtil.write("Claim creation");
		IOUtil.write("Enter book id for claim ");
		bookController.printBookList();
		int bookId = IOUtil.readInt();
		IOUtil.write("Enter amount of the claim");
		int claim = IOUtil.readInt();
		claimStorageService.submitClaim(new Claim(claimStorageService.generateUnexistedId(), bookStorageService.getBookById(bookId), claim));

	}

	
	public void printClaimInfo() {

		IOUtil.write("Enter claim Id to show");
		int id = IOUtil.readInt();
		while (claimStorageService.getClaimById(id)== null){
			IOUtil.write("Claim not existed");
			IOUtil.write("Enter claim's to show Id");
			id = IOUtil.readInt();
		}
		IOUtil.write(claimInfo(claimStorageService.getClaimById(id)));
		

	}

	public void updateClaim() {
		IOUtil.write("Enter claim's Id for modifying");
		int id = IOUtil.readInt(); 
		while (claimStorageService.getClaimById(id)== null){
			IOUtil.write("Claim not existed");
			IOUtil.write("Enter claim Id");
			id = IOUtil.readInt();
		}
		Claim claim = claimStorageService.getClaimById(id);
		IOUtil.write("Change claim amount ");

		if (!IOUtil.read().isEmpty() ){
			claim.setClaimAmount(IOUtil.readInt());
		}
		IOUtil.write(claimInfo(claimStorageService.getClaimById(id)));

	}

	public void deleteClaim(){
		IOUtil.write("Enter claim's id to remove ");
		int id = IOUtil.readInt();
		while (claimStorageService.getClaimById(id)== null){
			IOUtil.write("Claim not existed");
			IOUtil.write("Enter claim Id");
			id = IOUtil.readInt();
		}
		claimStorageService.removeClaim(id);
		IOUtil.write("Claim is deleted");


	}

	public void submitClaim() {
		IOUtil.write("Enter claim's to submit Id");
		int id = IOUtil.readInt();
		while (claimStorageService.getClaimById(id)== null){
			IOUtil.write("Claim not existed");
			IOUtil.write("Enter claim Id");
			id = IOUtil.readInt();
		}
		claimStorageService.submitClaim(claimStorageService.getClaimById(id));
		IOUtil.write("Claim is submitted");

	}

	public void saveClaimListToFile(){
        IOUtil.write("Enter directory and file name for saving");
		String file = IOUtil.read();
		if (file.isEmpty()){
			file = "d:\\file2.csv";
			IOUtil.write("Saved by default in d:\file1.txt");
			IOUtil.write("Enter directory and file name :");
		}
		claimStorageService.saveClaimListToFile(file);
		
	}
	
	public void readClaimListFromFile(){
		
			IOUtil.write("Enter directory and file name :");
			String file = IOUtil.read();
			if (file.isEmpty()){
				file = "d:\\file2.csv";
				IOUtil.write("Saved by default in d:\file2.csv");
				
				
			}
			   for (Claim c:claimStorageService.readClaims(file)){
				   claimStorageService.submitClaim(c);
				    
			   }
				
			}
	public static String claimInfo(Claim c){
		
		StringBuffer str = new StringBuffer();
		str.append(c.getId());
		str.append("  ");		
		str.append(c.getBook().getId());
		str.append(" ");
		str.append(c.getClaimAmount());
		
		return str.toString();
	}

		}


